var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
(function (factory) {
    if (typeof module === "object" && typeof module.exports === "object") {
        var v = factory(require, exports);
        if (v !== undefined) module.exports = v;
    }
    else if (typeof define === "function" && define.amd) {
        define(["require", "exports", "react", "common", "mobile/services/shoppingCartService", "mobile/services/shoppingService", "mobile/services/memberService", "components/common/base-component", "maishu-ui-toolkit", "services/service", "components/common/component"], factory);
    }
})(function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    const React = require("react");
    const common_1 = require("common");
    const shoppingCartService_1 = require("mobile/services/shoppingCartService");
    const shoppingService_1 = require("mobile/services/shoppingService");
    const memberService_1 = require("mobile/services/memberService");
    const base_component_1 = require("components/common/base-component");
    const ui = require("maishu-ui-toolkit");
    const service_1 = require("services/service");
    const component_1 = require("components/common/component");
    let ShoppingCart = class ShoppingCart extends base_component_1.BaseComponent {
        constructor(props) {
            super(props);
            this.shoppingCart = base_component_1.createService(shoppingCartService_1.ShoppingCartService);
            this.shop = base_component_1.createService(shoppingService_1.ShoppingService);
            this.member = base_component_1.createService(memberService_1.MemberService);
            this.state = {
                status: 'normal',
                items: shoppingCartService_1.ShoppingCartService.items.value,
                editSelectedItems: [],
            };
        }
        checkItem(item) {
            return __awaiter(this, void 0, void 0, function* () {
                if (this.state.status == 'edit') {
                    let { editSelectedItems } = this.state;
                    if (editSelectedItems.indexOf(item) >= 0) {
                        editSelectedItems = editSelectedItems.filter(o => o != item);
                    }
                    else {
                        editSelectedItems.push(item);
                    }
                    this.setState({ editSelectedItems });
                    return;
                }
                if (item.Selected == false)
                    yield this.shoppingCart.selectItem(item.Id);
                else
                    yield this.shoppingCart.unselectItem(item.Id);
            });
        }
        removeSelectedItems() {
            return __awaiter(this, void 0, void 0, function* () {
                let items = this.state.editSelectedItems;
                yield this.shoppingCart.removeItems(items.map(o => o.Id));
                this.setDeleteItems([]);
            });
        }
        decreaseCount(item) {
            let itemCount = item.Count; //this.state.inputCounts[item.Id] || 
            if (itemCount <= 1) {
                ui.confirm({
                    message: `确定要删除'${item.Name}'吗？`,
                    confirm: () => __awaiter(this, void 0, void 0, function* () {
                        yield this.shoppingCart.removeItems([item.Id]);
                        let deleteItems = this.state.editSelectedItems.filter(o => o.Id != item.Id);
                        this.setDeleteItems(deleteItems);
                    })
                });
                return;
            }
            this.changeItemCount(item, `${(itemCount) - 1}`);
        }
        increaseCount(item) {
            let itemCount = item.Count + 1;
            this.changeItemCount(item, `${itemCount}`);
        }
        changeItemCount(item, value) {
            let count = Number.parseInt(value);
            if (!count)
                return;
            item.Count = count;
            this.setState(this.state);
        }
        // cancel() {
        //     status.value = "normal";
        //     this.setState({ status: status.value });
        // }
        // edit() {
        //     if (this.state.status == 'normal') {
        //         // this.state.status = 'edit';
        //         // status.value = this.state.status;
        //         // this.setState(this.state);
        //         status.value = "edit";
        //         this.setState({ status: "edit" });
        //         return Promise.resolve();
        //     }
        //     let inputCounts = this.state.inputCounts;
        //     let itemIds = new Array<string>();
        //     let quantities = new Array<number>();
        //     for (let i = 0; i < this.state.items.length; i++) {
        //         let item = this.state.items[i];
        //         if (inputCounts[item.Id] != null && inputCounts[item.Id] != item.Count) {
        //             itemIds.push(item.Id);
        //             quantities.push(inputCounts[item.Id]);
        //         }
        //     }
        //     let result: Promise<any>;
        //     if (itemIds.length > 0) {
        //         result = this.shoppingCart.setItemsCount(itemIds, quantities);
        //     }
        //     else {
        //         result = Promise.resolve({});
        //     }
        //     result.then(() => {
        //         // this.state.status = 'normal';
        //         // status.value = this.state.status;
        //         // this.setState(this.state);
        //         status.value = "normal";
        //         this.setState({ status: status.value });
        //     });
        //     return result;
        // }
        checkAll() {
            if (this.state.status == 'normal') {
                return this.shoppingCart.selectAll();
            }
            this.setDeleteItems(this.state.items);
        }
        uncheckAll() {
            if (this.state.status == 'normal') {
                return this.shoppingCart.unselectAll();
            }
            this.setDeleteItems([]);
        }
        setDeleteItems(items) {
            this.setState({ editSelectedItems: items });
            // deleteItemsCount.value = items.length;
        }
        buy() {
            return __awaiter(this, void 0, void 0, function* () {
                if (this.state.selectedCount <= 0)
                    return;
                var items = this.state.items.filter(o => o.Selected);
                var productIds = items.map(o => o.ProductId);
                var quantities = items.map(o => o.Count);
                let userInfo = yield this.member.userInfo();
                let memberName = userInfo.Mobile || '';
                let order = yield this.shop.createOrder(memberName, productIds, quantities);
                this.app.redirect("shopping/orderProducts", { id: order.Id });
                return order;
            });
        }
        isChecked(item) {
            if (this.state.status == 'normal') {
                return item.Selected;
            }
            return this.state.editSelectedItems.indexOf(item) >= 0;
        }
        removeConfirmText() {
            let items = this.state.editSelectedItems;
            let str = "是否要删除？<br/> " + items.map(o => '<br/>' + o.Name);
            return str;
        }
        componentDidMount() {
            common_1.subscribe(this, shoppingCartService_1.ShoppingCartService.items, (value) => {
                this.setState({ items: value });
            });
        }
        render() {
            if (this.state.editSelectedItems == null || this.state.items == null)
                return React.createElement(React.Fragment, null);
            let items = this.state.items;
            console.assert(items != null);
            return items.length > 0 ?
                React.createElement("div", { className: "shopping-shoppingCart container" },
                    React.createElement("ul", { className: "list-group" }, items.map(o => React.createElement("li", { key: o.Id, className: "list-group-item row" },
                        o.Type == null ?
                            React.createElement("button", { onClick: () => this.checkItem(o), className: "pull-left icon" },
                                React.createElement("i", { className: this.isChecked(o) ? 'icon-ok-sign' : 'icon-circle-blank' })) : null,
                        React.createElement("div", { className: "pull-left pic", onClick: () => this.app.forward("home/product", { id: o.ProductId }) }, o.Type == 'Reduce' || o.Type == 'Discount' ?
                            React.createElement("div", { className: o.Type }, o.Type == 'Reduce' ? '减' : '折')
                            :
                                React.createElement("img", { src: service_1.imageUrl(o.ImagePath), style: { width: "100%", height: "100%" }, ref: (e) => e ? ui.renderImage(e) : null })),
                        React.createElement("div", { style: { marginLeft: 100 } },
                            React.createElement("div", { onClick: () => this.checkItem(o) }, o.Name),
                            React.createElement("div", { style: { height: 42, paddingTop: 4 } },
                                React.createElement("div", { className: "price pull-left", style: { marginTop: 10 } }, o.Price ? "￥" + o.Price.toFixed(2) : ""),
                                React.createElement("div", { className: "pull-right", style: { marginTop: 4 } }, this.state.status == 'normal' || o.Type != null ?
                                    React.createElement("div", { style: { paddingLeft: 6 } },
                                        "X ",
                                        o.Count)
                                    :
                                        React.createElement("div", { className: "input-group", style: { width: 120, display: 'table' } },
                                            React.createElement("span", { onClick: () => this.decreaseCount(o), className: "input-group-addon" },
                                                React.createElement("i", { className: "icon-minus" })),
                                            React.createElement("input", { value: `${o.Count}`, className: "form-control", type: "text", style: { textAlign: 'center' }, onChange: (e) => (this.changeItemCount(o, e.target.value)) }),
                                            React.createElement("span", { onClick: () => this.increaseCount(o), className: "input-group-addon" },
                                                React.createElement("i", { className: "icon-plus" }))))))))))
                :
                    React.createElement("div", { className: "norecords" },
                        React.createElement("div", { className: "icon" },
                            React.createElement("i", { className: "icon-shopping-cart" })),
                        React.createElement("h4", { className: "text" }, "\u4F60\u7684\u8D2D\u4E70\u8F66\u7A7A\u7A7A\u5982\u4E5F"));
        }
    };
    ShoppingCart = __decorate([
        component_1.component()
    ], ShoppingCart);
    exports.ShoppingCart = ShoppingCart;
});
